<?php
class TipsController extends CpController{
    public function init(){
        $url = new Url();
        $this->breadcrumbs[] = array('name'=>'Quản trị Tip','link'=>$url->createUrl("tips/index"),'class'=>'');
    }

    public function actionIndex(){
        $this->breadcrumbs[] = array('name'=>'Danh sách Tip','link'=>"#",'class'=>'active');
        $data = ATips::getAllData();
        $this->render("index",array("data"=>$data));
    }

    public function actionCreate(){
        $this->breadcrumbs[] = array('name'=>'Thêm mới tip','link'=>"#",'class'=>'active');
        $this->render("create");
    }
    public function actionAjaxQuickUpdate(){
        $time_now = time();
        $color = isset($_POST["color"]) ? trim($_POST["color"]):"";
        $icon = isset($_POST["icon"]) ? intval($_POST["icon"]):0;
        $button = isset($_POST["button"]) ? intval($_POST["button"]):0;
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if($id >0){
            $data_old = ATips::getDataById($id);
            $data = array(
            "color"=>array("value"=>$color,"type"=>2),
            "icon"=>array("value"=>$icon,"type"=>1),
            "button"=>array("value"=>$button,"type"=>1),
            "id"=>array("value"=>$id,"type"=>1)
            );

            $table = "c_tips";
            $result = CommonDB::updateRow($table,$data,array("id"));
            if($result >0){
                $act = 2;
                $obj = 6;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',name: '.$data_old["title"].',color: '.$data_old["color"].',icon: '.$data_old["icon"].'} thành';
                $content .= ' {id: '.$id.',name: '.$data_old["title"].',color: '.$color.',icon: '.$icon.',button: '.$button.'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
    public function actionAjaxSaveTips(){
        $time_now = time();
        $title_tip = isset($_POST["title_tip"]) ? trim($_POST["title_tip"]):"";
        $url = isset($_POST["url"]) ? trim($_POST["url"]):"";
        $position = isset($_POST["position"]) ? intval($_POST["position"]):0;
        $type = isset($_POST["type"]) ? intval($_POST["type"]):0;
        $color = isset($_POST["color"]) ? trim($_POST["color"]):"";
        $icon = isset($_POST["icon"]) ? intval($_POST["icon"]):0;
        $button = isset($_POST["button"]) ? intval($_POST["button"]):0;
        if($title_tip==""){
            echo "Tiêu đề được để trống";exit();
        }
        if($url==""){
            echo "Link không được để trống";exit();
        }
        $data = array(
        "title"=>array("value"=>$title_tip,"type"=>2),
        "url"=>array("value"=>$url,"type"=>2),
        "position"=>array("value"=>$position,"type"=>1),
        "type"=>array("value"=>$type,"type"=>1),
        "color"=>array("value"=>$color,"type"=>2),
        "icon"=>array("value"=>$icon,"type"=>1),
        "button"=>array("value"=>$button,"type"=>1),
        "create_date"=>array("value"=>$time_now,"type"=>1),
        );
        $table = "c_tips";
        $last_id = CommonDB::insertRow($table,$data);
        if($last_id >0){
            $act = 1;
            $obj = 6;
            $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$last_id.',name: '.$title_tip.',url: '.$url.'}';
            $result = AAdminLog::insertAdminLog($act,$obj,$content);
            echo 1;exit();
        }else{
            echo "Có lỗi trong quá trình xử lý";exit;
        }
    }
    public function actionEdit(){
        $id = isset($_GET["id"]) ? intval($_GET["id"]):0;
        $data = ATips::getDataById($id);
        if($data){
            $this->render("edit",array("data"=>$data));
        }
    }
    public function actionAjaxUpdateTips(){
        $time_now = time();
        $title_tip = isset($_POST["title_tip"]) ? trim($_POST["title_tip"]):"";
        $url = isset($_POST["url"]) ? trim($_POST["url"]):"";
        $position = isset($_POST["position"]) ? intval($_POST["position"]):0;
        $type = isset($_POST["type"]) ? intval($_POST["type"]):0;
        $color = isset($_POST["color"]) ? trim($_POST["color"]):"";
        $icon = isset($_POST["icon"]) ? intval($_POST["icon"]):0;
        $button = isset($_POST["button"]) ? intval($_POST["button"]):0;
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if($title_tip==""){
            echo "Tiêu đề được để trống";exit();
        }
        if($url==""){
            echo "Link không được để trống";exit();
        }
        if($id >0){
            $data_old = ATips::getDataById($id);
            $data = array(
            "title"=>array("value"=>$title_tip,"type"=>2),
            "url"=>array("value"=>$url,"type"=>2),
            "position"=>array("value"=>$position,"type"=>1),
            "type"=>array("value"=>$type,"type"=>1),
            "color"=>array("value"=>$color,"type"=>2),
            "icon"=>array("value"=>$icon,"type"=>1),
            "button"=>array("value"=>$button,"type"=>1),
            "id"=>array("value"=>$id,"type"=>1),
            );
            $table = "c_tips";
            $result = CommonDB::updateRow($table,$data,array("id"));
            if($result >0){
                $act = 2;
                $obj = 6;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',name: '.$data_old["title"].',url: '.$data_old["url"].',color: '.$data_old["color"].',icon: '.$data_old["icon"].'}';
                $content .= ' {id: '.$id.',name: '.$title_tip.',url: '.$url.',color: '.$color.',icon: '.$icon.',button: '.$button.'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit();
            }else{
                echo "Có lỗi trong quá trình xử lý";exit;
            }
        }
    }
    public function actionAjaxDelete(){
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0; 
        if($id >0){
            $data_old = ATips::getDataById($id); 
            $table = "c_tips";
            $result = CommonDB::deleteRow($table,array("id"=>$id));
            if($result >0){
                $act = 3;
                $obj = 6;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',name: '.$data_old["title"].',url: '.$data_old["url"].'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit();
            }else{
                echo "Có lỗi trong quá trình xử lý";exit;
            }
        }
    }
}
